﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;

namespace GWEB.WebFrame.Models
{
    public class ContractSp
    {
        public PageInfo Init()
        {
            PageInfo pg = new PageInfo();
            pg.Type = "Contract";
            pg.Title = "合同中心";
            pg.AddText = "新增合同";
            pg.EditText = "修改";
            pg.LocateText = "查询";
            pg.SMenuId = "HT01";
            pg.FileUrl = "../Contract/ContractList";
            pg.Condition = "";
            pg.TableName = "cu_Contract";
            pg.KeyFieldName = "id";
            //pg.PopEditHeight = 650;
            //pg.PopEditWidth = 860;
            HttpSessionState session = System.Web.HttpContext.Current.Session;
            pg.dict = BasePage.getDict(session);
            DataRow drUser = BasePage.GetUser(session);
            String leagueId = Fun.cString(drUser["LeagueId"].ToString());
            pg.Sop = BasePage.Grant(session, pg.SMenuId);
            pg.Sop.EDIT_htm = " ";
            pg.Sop.DEL = false;
            pg.Sop.ADD = false;


            string Url = "../Contract/ContractList/UpdateState";//convert(varchar(10), cu_Account.id)        
            String sqlCount = "select count(*) count from  cu_Contract where   LeagueId=" + leagueId;
            String Sql = @"select cu_Contract.id, custname ,LinkName, cu_Customers.CustTel,contractNumber,case when (select IsClassHour from cu_Contract_Class where cu_Contract_Class.id=Setmealid)=0 then convert(varchar(10),ConEnd,120) else convert(varchar(10),SurplusNumber) end Surplus ,
            convert(varchar(10),ContractPrice)+'/'+convert(varchar(10),DisPrice) Price,convert(varchar(10),constart,120)+'-'+convert(varchar(10),conend,120) ConSEnd,AchievementUser.sshowname AchievName,
  case cu_Contract.state when 1 then '<a class=''layui-btn  layui-btn-xs'' href=""javascript: Do(''" + Url + @"'',{ id: '+ltrim(cu_Contract.id)+',keyCode: GetKeyCode('''+ltrim(cu_Contract.id)+'''),state:2})"" >通过</a><a class=''layui-btn layui-bg-red layui-btn-xs'' href=""javascript: Do(''" + Url + @"'',{ id: '+ltrim(cu_Contract.id)+',keyCode: GetKeyCode('''+ltrim(cu_Contract.id)+'''),state:3})"" >不通过</a>'   when 2 then  '<a class=''layui-btn  layui-btn-xs'' href=""javascript:ShowModalUrl(''popEdit'', ''../Account/At?id=-1&Cid='+ltrim(cu_Contract.Cid)+'&Contractid='+ltrim(cu_Contract.id)+'&FlowingType=00202001&keyCode=''+GetKeyCode('+ltrim(cu_Contract.id)+'), ''退费'', '''', '''', '''')"" >退费</a><a class=''layui-btn layui-btn-xs'' href=""javascript:ShowModalUrl(''popEdit'', ''../Customer/ContractEditDate?id='+ltrim(cu_Contract.id)+'&keyCode=''+GetKeyCode('+ltrim(cu_Contract.id)+'), ''延期'', '''', ''600'', ''400'')"" >延期</a><a class=''layui-btn layui-btn-xs'' href=""javascript:ShowModalUrl(''popEdit'', ''../Contract/Carried?id='+ltrim(cu_Contract.id)+'&keyCode=''+GetKeyCode('+ltrim(cu_Contract.id)+'), ''执行'', '''', ''400'', ''450'')"" >执行</a>'      end as link,
                         case status when 0 then  '已登记未收费' when 1 then '已登记费用未入账' when 2 then '未开始执行' when 3 then '执行中' when 4 then '延期中' when 5 then '已登记退费未出账' when 6 then '已退费' end statusName,
            
            CardNumber,contracttype,SetmealId,SetmealName,ContractPrice,DisPrice,cu_Contract.ContractDate,Constart,ConEnd,Remark,MaxLeave,MaxMakeUP,ConCourseNumber,GiveCourseNumber,isnull(Gift1,'') Gift1,isnull(Gift1Price,0) Gift1Price,isnull(Gift2,'') Gift2,isnull(Gift2Price,0) Gif2Price,isnull(Gift3,'') Gift3,isnull(Gift3Price,0) Gift3Price,state,status,cu_Contract.CreateId,cu_Contract.CreateDate,AuditingId,AuditingDate,AuditingRemark,AchievementId,cu_Contract.LeagueId from cu_Contract
               left join cu_Customers on cu_Contract.cId = cu_Customers.id
              left join tb_sys_users AchievementUser on AchievementUser.iuserid = cu_Contract.AchievementId
            where cu_Contract.LeagueId = " + leagueId;
            Dictionary<String, String> tab = new Dictionary<string, string>();
            tab.Add("0", "全部");
            tab.Add("1", "本周新签");
            tab.Add("2", "本月新签");
            tab.Add("3", "30日内到期");
            tab.Add("4", "延期合同");
            tab.Add("5", "从未排课");
            tab.Add("6", "已完结");
            tab.Add("7", "审批未通过");
            DataView dv = new DataView();
            foreach (var dr in tab)
            {
                string count = "";
                sqlCount = "select count(*) count from  cu_Contract where   LeagueId=" + leagueId + Constring(dr.Key);
                dv = BasePage.bd.ExecuteTable(sqlCount).DefaultView;
                if (dv.Count == 1)
                    count = dv[0]["count"].ToString();
                if (count != "" && int.Parse(count) > 99)
                    count += "+";
                if (count != "")
                    count = "<span class='layui-badge'>" + count + "</span>";
                pg.Tab.Add(dr.Value + count, "KeyFocusId='';TabIndex=" + dr.Key + ";LocatePara='';DataPageUrl='../Main/DataPageLay?type=ContractSp&state=" + dr.Key + "';GoPage(1);");
            }

            HttpRequest request = System.Web.HttpContext.Current.Request;
            String state = Fun.cString(request["state"]);


            pg.Sql = Sql + Constring(state);
            ColumnInfo cl;
            cl = new ColumnInfo(); cl.FieldName = "contractNumber"; cl.HeadText = "合同编号"; cl.Visible = true; cl.IsCondition = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "custname"; cl.HeadText = "学员姓名"; cl.Visible = true; cl.IsCondition = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "CustTel"; cl.HeadText = "联系电话"; cl.Visible = true; cl.IsCondition = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "SetmealName"; cl.HeadText = "合同套餐"; cl.Visible = true; cl.DisplayFormat = "{0:dd / MM / yyyy}"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "Surplus"; cl.HeadText = "课程剩余"; cl.Visible = true; cl.IsSum = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "Price"; cl.HeadText = "应收/实收金额"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "GiveCourseNumber"; cl.HeadText = "赠送课时"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "ContractDate"; cl.HeadText = "签约日期"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "ConSEnd"; cl.HeadText = "合同有效期"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "AchievName"; cl.HeadText = "业绩所属人"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "contracttype"; cl.HeadText = "合同类型"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "statusName"; cl.HeadText = "合同状态"; cl.Visible = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.FieldName = "link"; cl.HeadText = "操作"; cl.IsSort = false; pg.AddColumn(cl);
            return pg;
        }
        public String Constring(string Value)
        {
            String C = "";
            switch (Value)
            {
                case "0":
                    C = " and state>=1";
                    break;
                case "1":
                    C = " and state>=1 and  cu_Contract.CreateDate>=DATEADD(d,-7,getdate())";
                    break;
                case "2":
                    C = " and state>=1 and  cu_Contract.CreateDate>=DATEADD(d,-30,getdate())";
                    break;
                case "3":
                    C = " and state>=1 and ConEnd<=DATEADD(d,30,getdate()) and ConEnd>GETDATE()";
                    break;
                case "4":
                    C = " and status=4";
                    break;
                case "5":
                    C = " and state>=1 and CourseStart is null";
                    break;
                case "6":
                    C = " and (isnull(SurplusNumber,0)-isnull(GiveCourseNumber,0)<=0 or isnull(CourseEnd,'2099-12-31')<=GETDATE()) ";
                    break;
                case "7":
                    C = " and state=3 ";
                    break;
            }
            return C;
        }
    }
}